import axios from 'axios'

// 通用公用方法
const req = (method, url, params) => {
    return axios({
        method: method,
        url: url,
        headers: {
            satoken: localStorage.getItem('logintoken')
        },
        data: params,
        traditional: true
    }).then(res => {
        const data = res.data
        if(data.status && data.status === 401){
            setTimeout(() => {
                window.location.href = '/login'
            }, 1000)
        }
        return data
    })
}

// 上传文件
const reqUpload = (url, params) => {
    return axios({
        method: 'post',
        url: url,
        headers: {
            satoken: localStorage.getItem('logintoken'),
            'Content-Type': 'multipart/form-data'
        },
        data: params,
        traditional: true
    }).then(res => res.data)
}

export {
    req,
    reqUpload
}